package com.question.business.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.question.business.user.model.dto.UserCacheDTO;
import com.question.business.user.repository.IUserCacheRepository;
import com.question.common.annotation.DataIgnoreConvert;
import com.question.common.annotation.UserConvert;
import com.question.common.annotation.UserIgnoreConvert;
import com.question.common.constants.StringConstants;
import com.question.common.enums.DataIgnoreConvertEnum;
import com.question.common.model.dto.DataTypeConvertDTO;
import com.question.common.service.IDataConvert;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;

/**
 * 用户转换实现类
 *
 * @author star
 * @since 2023/9/11
 */
@Slf4j
@Service
@AllArgsConstructor
public class UserDataConvertImpl implements IDataConvert {

    private IUserCacheRepository userCacheRepository;

    /**
     * 数据转换
     *
     * @param sourceFieldName 源属性
     * @param annotation      注解信息
     * @param value           源数据
     * @return String
     */
    @Override
    public String convert(String sourceFieldName, Annotation annotation, Object value) {
        if (Objects.nonNull(value)) {
            if (value instanceof List) {
                StringJoiner joiner = new StringJoiner(",");
                List list = (List) value;
                for (Object id : list) {
                    if (StringConstants.SYSTEM.equals(id)) {
                        joiner.add(StringConstants.SYSTEM_DESC);
                    } else {
                        UserCacheDTO user = userCacheRepository.queryById(String.valueOf(id));
                        if (Objects.nonNull(user) && StringUtils.isNotBlank(user.getNickName())) {
                            joiner.add(user.getNickName());
                        }
                    }
                }
                return joiner.toString();
            } else {
                if (StringConstants.SYSTEM.equals(value)) {
                    return StringConstants.SYSTEM_DESC;
                } else {
                    UserCacheDTO user = userCacheRepository.queryById(String.valueOf(value));
                    if (Objects.nonNull(user) && StringUtils.isNotBlank(user.getNickName())) {
                        return user.getNickName();
                    }
                }
            }
        }
        return null;
    }

    @Override
    public DataTypeConvertDTO confirmType(Field field) {
        UserConvert userConvert = field.getAnnotation(UserConvert.class);
        if (Objects.nonNull(userConvert)) {
            String targetFieldName = userConvert.target();
            if (StringUtils.isBlank(targetFieldName)) {
                targetFieldName = field.getName().concat("Name");
            }
            return DataTypeConvertDTO.builder()
                    .annotation(userConvert)
                    .dataConvert(this)
                    .targetField(targetFieldName)
                    .build();
        }
        return null;
    }


    /**
     * 数据转换忽略
     *
     * @param ignoreEnum  方法注解信息
     * @param sourceField 字段信息
     * @return boolean
     */
    @Override
    public boolean isIgnore(DataIgnoreConvertEnum ignoreEnum, Field sourceField) {
        DataIgnoreConvert dataIgnoreConvert = sourceField.getAnnotation(DataIgnoreConvert.class);
        boolean isDataIgnore = Objects.nonNull(dataIgnoreConvert) && DataIgnoreConvertEnum.USER.equals(dataIgnoreConvert.type());
        UserIgnoreConvert userIgnoreConvert = sourceField.getAnnotation(UserIgnoreConvert.class);
        return DataIgnoreConvertEnum.USER.equals(ignoreEnum) || isDataIgnore || Objects.nonNull(userIgnoreConvert);
    }

}
